{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名 孟媛  \n",
    "+ 学号 0165339"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I'm holding on your rope\n",
      "Got me ten feet off the ground\n",
      "I'm hearing what you say but I just can't make a sound\n",
      "It's too late to apologize, it's too late\n",
      "I said it's too late to apologize, it's too late\n",
      "It's too late to apologize, it's too late\n",
      "I said it's too late to apologize, it's too late\n",
      "\n",
      "You tell me that you need me\n",
      "Then you go and cut me down, but wait\n",
      "You tell me that you're sorry\n",
      "Don't think I'd turn around, and say\n",
      "It's too late to apologize, it's too late\n",
      "I said it's too late to apologize, it's too late\n",
      "It's too late to apologize, it's too late\n",
      "I said it's too late to apologize, it's too late\n",
      "It's too late to apologize, it's too late\n",
      "I said it's too late to apologize, it's too late\n"
     ]
    }
   ],
   "source": [
    "print(\"\\nI'm holding on your rope\\nGot me ten feet off the ground\\nI'm hearing what you say but I just can't make a sound\")\n",
    "def B():\n",
    "    print(\"It's too late to apologize, it's too late\\nI said it's too late to apologize, it's too late\")\n",
    "B()\n",
    "B()\n",
    "print(\"\\nYou tell me that you need me\\nThen you go and cut me down, but wait\\nYou tell me that you're sorry\\nDon't think I'd turn around, and say\")\n",
    "B()\n",
    "B()\n",
    "B()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=13\n",
      "样本均值为5.0\n",
      "样本方差为6.76923076923077\n",
      "n=13\n",
      "样本均值为7.230769230769231\n",
      "样本方差为16.023668639053255\n"
     ]
    }
   ],
   "source": [
    "def statistics(*figure):\n",
    "    count=0\n",
    "    sum=0\n",
    "    summean=0\n",
    "    for value in figure:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "        samplemean=sum/count\n",
    "        summean+=value**2\n",
    "        samplevar=summean/count-samplemean**2\n",
    "    print(\"n=%s\"%count)\n",
    "    print(\"样本均值为%s\"%samplemean)\n",
    "    print(\"样本方差为%s\"%samplevar)\n",
    "statistics(1,3,5,7,9,7,5,3,1,3,5,7,9)\n",
    "statistics(2,4,6,8,10,12,14,12,10,1,3,5,7,)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=13\n",
      "样本均值为5.0\n",
      "样本方差为6.76923076923077\n",
      "n=12\n",
      "样本均值为7.583333333333333\n",
      "样本标准差为3.882832585740581\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def statistics(whether,*figure):\n",
    "    count=0\n",
    "    sum=0\n",
    "    summean=0\n",
    "    for value in figure:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "        samplemean=sum/count\n",
    "        summean+=value**2\n",
    "        samplevar=summean/count-samplemean**2\n",
    "        standardeviation=0\n",
    "    print(\"n=%s\"%count)\n",
    "    print(\"样本均值为%s\"%samplemean)\n",
    "    if whether==0:\n",
    "        print(\"样本方差为%s\"%samplevar)\n",
    "    else:\n",
    "        standarddeviation=math.sqrt(samplevar)\n",
    "        print(\"样本标准差为%s\"%standarddeviation)\n",
    "statistics(0,1,3,5,7,9,7,5,3,1,3,5,7,9)\n",
    "statistics(2,4,6,8,10,12,14,12,10,1,3,5,6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n=10\n",
      "样本均值为5.6\n",
      "样本方差为6.440000000000001\n",
      "最大值为9\n",
      "最小值为1\n",
      "样本标准误为2.5377155080899043\n",
      "\n",
      "\n",
      "n=9\n",
      "样本均值为8.222222222222221\n",
      "样本方差为17.728395061728406\n",
      "最大值为14\n",
      "最小值为1\n",
      "\n",
      "\n",
      "n=8\n",
      "样本均值为6.5\n",
      "样本方差为5.25\n",
      "最小值为2\n",
      "\n",
      "\n",
      "n=10\n",
      "样本均值为5.8\n",
      "样本方差为7.759999999999998\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def statistics(meanerror=None,max=None,min=None,*figure):\n",
    "    count=0\n",
    "    sum=0\n",
    "    summean=0\n",
    "    for value in figure:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "        samplemean=sum/count\n",
    "        summean+=value**2\n",
    "        samplevar=summean/count-samplemean**2\n",
    "    print(\"n=%s\"%count)\n",
    "    print(\"样本均值为%s\"%samplemean)\n",
    "    print(\"样本方差为%s\"%samplevar)  \n",
    "    \n",
    "    if max:\n",
    "        for value in figure:\n",
    "            if value > max:\n",
    "                max=value\n",
    "        print(\"最大值为%s\"%max)\n",
    "    if min:\n",
    "        for value in figure:\n",
    "            if value < min:\n",
    "                min=value\n",
    "        print(\"最小值为%s\"%min)\n",
    "    if meanerror:\n",
    "        for value in figure:\n",
    "            count+=1\n",
    "            sum+=value\n",
    "            samplemean=sum/count\n",
    "            summean+=value**2\n",
    "            samplevar=summean/count-samplemean**2\n",
    "            meanerror=math.sqrt(samplevar)\n",
    "        print(\"样本标准误为%s\"%meanerror)\n",
    "statistics(1,3,5,7,9,7,5,3,1,3,5,7,9)\n",
    "print(\"\\n\")\n",
    "statistics(0,6,8,10,12,14,12,10,1,3,5,7,)\n",
    "print(\"\\n\")\n",
    "statistics(0,0,2,3,4,5,6,7,8,9,10)\n",
    "print(\"\\n\")\n",
    "statistics(0,0,0,1,2,3,9,9,8,8,7,6,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
